﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace information
{
    public partial class DataInput : Form
    { 
        public string TimeValue;  //时间数据
        public DataInput()
        {
            InitializeComponent();
        }

        private void DataInput_Load(object sender, EventArgs e)
        {

        }


        //性别按钮判断
        public string  radioButtonChange( )
        {
            if (radioButton1.Checked == true)
            {
                return "男";
            }
            else if (radioButton2.Checked == true)
            {
                return "女";
            }
            else return "性别没有选择";
        }



        private void button_submit_Click(object sender, EventArgs e)
        {
            //数据库连接
            string strcon;
            strcon = "Server=192.168.0.88; Database=information;User id ='sa';pwd=123456";  //information            
            SqlConnection myon = new SqlConnection(strcon);
            myon.Open();
            MessageBox.Show("数据库连接成功", "数据库");

            //数据获取
            string NameValue = textBox_name.Text;
            string SexValue = radioButtonChange();


            //数据写入数据库
            string strsql;
            strsql = "insert into InformationTable(name,sex,time)values(N'" + NameValue + "','" + SexValue + "','" + TimeValue+ "')"; //向Information表的name列写入textBox_name.Text
            SqlCommand sqlcom = new SqlCommand(strsql, myon);  //name为 InformationTable表中的列名
            sqlcom.ExecuteNonQuery();  //判断操作是否成功,返回影响行数 大于0 成功  等于0 插入删除和更新操作失败
            MessageBox.Show("添加成功！", "数据");

            //关闭数据库
            myon.Close();

        }


        //性别选择按钮
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                radioButton2.Checked = false;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                radioButton1.Checked = false;
            }
        }


        //获取出生时间
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime date = DateTime.Parse(dateTimePicker1.Text); //结构体类型DateTime parse将dateTimePicker1.Text字符串转化为结构体中的各个变量值，struct可看作是缩小的class，适宜小数据成员时使用
            TimeValue = date.Date.ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }









    }
}
